<template>
  <el-dialog title="常用地址管理" :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-autocomplete
        v-model="state"
        popper-class="my-autocomplete"
        :fetch-suggestions="querySearch"
        placeholder="请输入内容"
        @select="handleSelect"
      >
        <i slot="suffix" class="el-icon-edit el-input__icon" @click="handleIconClick" />
        <template slot-scope="{ item }">
          <div class="name">{{ item.value }}</div>
          <span class="addr">{{ item.address }}</span>
        </template>
      </el-autocomplete>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="queryBtn(0)">取 消</el-button>
      <el-button type="primary" @click="queryBtn(1)">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import adminSupport from 'lola-admin-support-el'
export default {
  props: {
    requestUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {},
      restaurants: [],
      state: '',
      selectItem: {}, // 已选择列表
      locationType: '' // 地址类型
    }
  },
  mounted() {
    this.loadAll()
  },
  methods: {
    //  确定取消事件
    queryBtn(type) {
      this.dialogFormVisible = false
      this.state = ''
      if (type === 1) {
        this.selectItem['locationType'] = this.locationType
        this.$emit('handleAddress', this.selectItem)
      }
    },

    modelShow(type) {
      console.log(type)
      this.locationType = type
      this.selectItem = {}
      this.dialogFormVisible = true
    },
    querySearch(queryString, cb) {
      var restaurants = this.restaurants
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        )
      }
    },
    loadAll() {
      const that = this
      const url = adminSupport.lib.service.conf.api.get('wms').url + this.requestUrl + '/1?key='
      that.$http
        .post(url)
        .then(function(rsp) {
          console.log(rsp)
          const list = rsp.data
          list.forEach((val, index) => {
            val['value'] = val['area']
          })
          console.log(list)
          that.restaurants = list
        })
        .catch(function(err) {
          console.log(err)
        })
    },
    handleSelect(item) {
      console.log(item)
      this.selectItem = item
    },
    handleIconClick(ev) {
      console.log(ev)
    }
  }
}
</script>

<style lang="less" scoped>
.el-autocomplete {
  width: 70%;
}
.my-autocomplete {
  // width: 80%;
  li {
    line-height: normal;
    padding: 7px;

    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .addr {
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
</style>
